/**
 * 
 */
package com.dalonedrow.engine.systems;

import com.dalonedrow.utils.UtilityMethods;

/**
 * A Timer class.
 * @author Donald
 */
public final class Timer {
	/** the length of time the <code>Timer</code> should run, in nanoseconds. */
	private long	duration;
	/**
	 * the time the <code>Timer</code>'s stopwatch was started, in nanoseconds.
	 */
	private long	stopwatchStart;
	/** the time the <code>Timer</code> was started, in nanoseconds. */
	private long	timeStart;
	/**
	 * Creates a new instance of Timer.java.
	 */
	public Timer() {
		timeStart = Time.getInstance().getGameTime();
		reset();
	}
	/**
	 * Gets the time the {@link Timer} was started, in milliseconds.
	 * @return long
	 */
	public long getStartTimeMillis() {
		return UtilityMethods.getInstance().convertNanoToMilli(timeStart);
	}
	/** Resets the stopwatch. */
	public void reset() {
		stopwatchStart = Time.getInstance().getGameTime();
	}
	/**
	 * Sets the duration.
	 * @param val the long to set
	 */
	public void setDuration(final long val) {
		duration = val;
	}
	/**
	 * Determines if the {@link Timer}'s stopwatch should be stopped.
	 * @param time the current time in nanoseconds
	 * @return true if the current time is equal to or after the {@link Timer}'s
	 *         duration; false otherwise
	 */
	public boolean stopwatch(final long time) {
		boolean stop = false;
		if (time > stopwatchStart + duration) {
			stopwatchStart = Time.getInstance().getGameTime();
			stop = true;
		}
		return stop;
	}
}
